let order_type = -1; //查询所有订单
layui.config({
    version: '1510164124181'
}).use(['form', 'laypage', 'layer', 'element', 'laydate'], function() {
    form = layui.form;
    layer = layui.layer;
    $ = layui.jquery;
    laypage = layui.laypage; //分页
    element = layui.element; //元素操作
    laydate = layui.laydate; //时间选择器
    //日期时间范围
    laydate.render({
        elem: '#order_time',
        type: 'datetime',
        range: true
    });
    $(".mystate a").click(function() {
        $(".mystate a").removeClass("red");
        $(this).addClass("red");
        SearchInfo();
    });
    inituserdata();
});

function ClearSearchInfo() {
    $("#phone").val(""); //用户手机号
    $("#express_num").val(""); //下单号
    $("#pro_name").val(""); //宝贝名称
    $("#order_type").val(""); //订单类型
    $("#order_state").val(""); //订单状态
    $("#order_time").val(""); //下单时间
    $("#orderId").val(""); //订单号
}

function ExcelPrintOrder() {

    let phone = $("#phone").val(); //用户手机号
    let express_num = $("#express_num").val(); //下单号
    let pro_name = $("#pro_name").val(); //宝贝名称

    let order_types = $("#order_type").val(); //订单类型

    let order_state = order_type;
    if (order_type == -1) { //所有订单列表里
        order_state = $("#order_state").val(); //订单状态
    }
    let order_time = $("#order_time").val(); //下单时间
    let startTime = order_time.split(" - ")[0];
    let endTime = order_time.split(" - ")[1];
    let orderId = $("#orderId").val(); //订单号
    window.location.href = httpurl + "/OrderExportExcel?phone=" + phone + "&express_num=" + express_num + "&order_types=" + order_types + "&order_type=" + order_type + "&pro_name=" + pro_name + "&order_state=" + order_state + "&startTime=" + startTime + "&endTime=" + endTime + "&orderId=" + orderId + "&isfree=-1";


}

function inituserdata() {
    // var index = top.layer.msg('加载中，请稍候',{icon: 16,time:false,shade:0.8});
    $("#tableInfo tbody").html("");
    let phone = $("#phone").val(); //用户手机号
    let express_num = $("#express_num").val(); //下单号
    let pro_name = $("#pro_name").val(); //宝贝名称

    let order_types = $("#order_type").val(); //订单类型

    let order_state = order_type;
    if (order_type == -1) { //所有订单列表里
        order_state = $("#order_state").val(); //订单状态
    }
    let order_time = $("#order_time").val(); //下单时间
    let startTime = order_time.split(" - ")[0];
    let endTime = order_time.split(" - ")[1];
    let orderId = $("#orderId").val(); //订单号
    $.ajax({
        url: httpurl + "/findOrderListPc",
        cache: false,
        headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "*",
            "Access-Control-Allow-Headers": "Authorization",
            "Authorization": GetTokenInfo()
        },
        contentType: 'application/json',
        data: {
            "pageNo": pageNo,
            "pageSize": pageRecords,
            "phone": phone,
            "express_num": express_num,
            "order_type": order_types,
            "pro_name": pro_name,
            "order_state": order_state,
            "startTime": startTime,
            "endTime": endTime,
            "orderId": orderId,
            "isfree": -1 //不要
        },
        success: function(data) {
            layer.closeAll();
            let dataHtml = "";
            if (data.rtnCode == 200) {
                let initdata = data.data.list;
                totalRecords = data.data.total;
                for (let i = 0; i < initdata.length; i++) {
                    let ClassInfo = "text-primary";
                    if (initdata[i].type == 1) {
                        ClassInfo = "text-success";
                    } else if (initdata[i].type == 2) {
                        ClassInfo = "text-warning";
                    }



                    dataHtml += '<tr class="shop">' +
                        '<td colspan="9" class="my-order-num">' +
                        '<span><input type="checkbox" class="aceParent" id="all"/></span>' +
                        '<a href="DetaileOrder.html?OrderId=' + initdata[i].orderInfo[0].orderEntity.id + '"><span>订单号：' + initdata[i].orderInfo[0].orderEntity.id + '</span></a>&nbsp;&nbsp;&nbsp;&nbsp;' +
                        '<span>创建时间：' + initdata[i].intime + '</span>&nbsp;&nbsp;&nbsp;&nbsp;' +
                        '<span>支付时间：' + initdata[i].paytime + '</span>&nbsp;&nbsp;&nbsp;&nbsp;' +
                        '</td>' +
                        '</tr>';
                    let orderList = initdata[i].orderInfo;
                    for (let a = 0; a < orderList.length; a++) {
                        let wlinfo = "";
                        let ostate = initdata[i].orderInfo[0].orderEntity.state;
                        let tempinfo = "";
                        if (ostate == 1) {
                            tempinfo = '<a href="#" onclick="UpdateOrderState(\'' + initdata[i].orderInfo[0].orderEntity.id + '\',6);">备货</a>&nbsp;&nbsp;&nbsp;';
                        }
                        if (ostate == 2 || ostate == 3) {
                            wlinfo = '<span class="text-info" onclick="SelectExpressInfo(' + initdata[i].express_num + ');">查看物流</span><br/>';
                        }
                        let ostates = (ostate == 0 ? '待付款' : (ostate == 1 ? '待发货' : (ostate == 2 ? '待收货' : (ostate == 3 ? '已收货' : (ostate == 4 ? '售后' : (ostate == 5 ? '已取消' : (ostate == 6 ? '备货' : "未知")))))));
                        dataHtml += '<tr class="item border-dashed">' +
                            '<td class="s-title">' +
                            '<a href="#" class="J_MakePoint">' +
                            '<img src="' + visitimgurl + '/' + orderList[a].productEntity.headimg + '" class="itempic"><span class="title J_MakePoint">' + orderList[a].productEntity.title + '</span></a>' +
                            '<div class="props">' +
                            '<span>套餐: ' + orderList[a].setMealEntity.meal + ' </span>' +
                            '<span>数量: ' + orderList[a].num + ' </span>' +
                            '</div>' +
                            '<a title="消费者保障服务，卖家承诺商品如实描述" href="#" target="_blank">' +
                            '<img src="http://img03.taobaocdn.com/tps/i3/T1bnR4XEBhXXcQVo..-14-16.png"/>' +
                            '</a>' +
                            '<div>' +
                            '<span style="color:gray;">卖家承诺72小时内发货</span>' +
                            '</div>' +
                            '</td>' +
                            '<td>￥' + orderList[a].setMealEntity.newprice + '</td>' +
                            '<td>' + orderList[a].num + '</td>' +
                            '<td>' + orderList[a].orderEntity.phone + '</td>' +
                            '<td>' + ostates + '</td>' +
                            '<td>' + orderList[a].orderEntity.couponid + '</td>' +
                            '<td>' +
                            '<span>￥' + parseFloat(orderList[a].setMealEntity.newprice * orderList[a].num).toFixed(2) + '</span><br/>' +
                            '<a  href="DetaileOrder.html?OrderId=' + initdata[i].orderInfo[0].orderEntity.id + '">查看详情</a><br/>' +
                            '<span>(含快递:￥' + orderList[a].setMealEntity.freight + ')</span><br/>' + wlinfo +
                            '</td>' +
                            '<td>' +
                            '<a href="DetaileOrder.html?OrderId=' + initdata[i].orderInfo[0].orderEntity.id + '">编辑</a>&nbsp;&nbsp;&nbsp;' + tempinfo +
                            '<a href="#" onclick="UpdateOrderState(\'' + initdata[i].orderInfo[0].orderEntity.id + '\',5);">取消</a>&nbsp;&nbsp;&nbsp;' +
                            '<a href="#" data-id="' + initdata[i].orderInfo[0].orderEntity.id + '"  data-info="deleteOrderById" onclick="DeleteInfo(this);">删除</a>' +
                            '</td>' +
                            '</tr>';
                    }

                }

            } else {
                totalRecords = 0;
                dataHtml += '<tr>' +
                    '<td colspan="12" style="text-align:center;">暂无数据</td>' +
                    '</tr>';
            }
            $("#tableInfo tbody").html(dataHtml);
            page();

        }

    });

}

function SelectExpressInfo(expNo) {
    $.ajax({
        url: httpurl + "/kdniaoTrack",
        cache: false,
        headers: {
            "Access-Control-Allow-Origin": "*",
            "Access-Control-Allow-Methods": "*",
            "Access-Control-Allow-Headers": "Authorization",
            "Authorization": GetTokenInfo()
        },
        contentType: 'application/json',
        data: {
            "expNo": expNo,
        },
        success: function(data) {
            // console.log(JSON.stringify(data)+"=====data");
            let dataInfo = data.data.traces;
            let info = "";
            if (data.ret == 200) {
                if (dataInfo.length > 0) {
                    for (let i = 0; i < dataInfo.length; i++) {
                        info += '<li class="layui-timeline-item">' +
                            '<i class="layui-icon layui-timeline-axis">&#xe63f;</i>' +
                            '<div class="layui-timeline-content layui-text">' +
                            '<h3 class="layui-timeline-title">' + dataInfo[i].time + '</h3>' +
                            ' <p>' + dataInfo[i].desc + '</p>' +
                            '</div>' +
                            '</li>';
                    }
                } else {
                    info += '暂无数据!!!';
                }
                layer.open({
                    type: 1,
                    area: ['600px', '500px'],
                    shadeClose: false, //点击遮罩关闭
                    content: '<ul class="layui-timeline" style="width: 50%;margin: 0 auto">' + info + '</ul>'
                });
            }

        }
    });
}

function UpdateOrderState(OrderId, state) {

    top.layer.confirm('确认提交？', { icon: 3, title: '提示信息' }, function(index) {
        var index = top.layer.msg('提交中，请稍候', { icon: 16, time: false, shade: 0.8 });
        $.ajax({
            url: httpurl + "/UpdateOrderEntityState",
            cache: false,
            headers: {
                "Access-Control-Allow-Origin": "*",
                "Access-Control-Allow-Methods": "*",
                "Access-Control-Allow-Headers": "Authorization",
                "Authorization": GetTokenInfo()
            },
            contentType: 'application/json',
            data: {
                "id": OrderId,
                "state": state
            },
            success: function(data) {
                layer.closeAll();
                top.layer.msg(data.msg);
                inituserdata();
            }
        });
    }, function() {

    });

}

function SearchInfo() {
    pageNo = 1;
    $(".mystate a").each(function() {
        if ($(this).hasClass("red")) {
            order_type = $(this).attr("data-id");
        }
    })
    console.log("order_type====" + order_type);
    inituserdata();
}

//分页
function page() {
    laypage.render({
        elem: 'page' //注意，这里的 test1 是 ID，不用加 # 号
            ,
        count: totalRecords //数据总数，从服务端得到
            ,
        curr: pageNo //获取起始页 location.hash.replace('#!fenye=', '')
            ,
        hash: 'fenye' //自定义hash值
            ,
        layout: ['count', 'prev', 'page', 'next', 'skip'],
        limit: pageRecords,
        jump: function(obj, first) {
            //首次不执行
            if (!first) {
                pageNo = obj.curr;
                pageRecords = obj.limit; // 当前页面记录数
                inituserdata();
            }
        }
    });
}